package com.fept.msgcore.msgserver.protocol.model.body;

import com.fept.msgcore.msgserver.protocol.model.base.ProtocolConstants;

/**
 * Created by Srma on 2/9/2017.
 */
public class DataBody extends BaseBody {


    /**
     *
     */
    private static final long serialVersionUID = -7384511962467349803L;

    private int eventSource;
    private int eventType;
    private String eventJson;

    public DataBody() {
        this.setType(ProtocolConstants.BODY_DATA);
    }

    /**
     *
     */
    @Override
    public byte[] pack() {
        byte[] eventJsonBytes = eventJson.getBytes();
        byte[] data = new byte[eventJsonBytes.length];
//        data[0] = (byte) (this.eventSource & 0xFF);
//        data[1] = (byte) ((this.eventType >> 8) & 0xFF);
//        data[2] = (byte) ((this.eventType) & 0xFF);

        System.arraycopy(eventJsonBytes, 0, data, 0, eventJsonBytes.length);

        this.setValue(data);
        this.setLength(data.length);

        return super.pack();
    }

    @Override
    public void unpack(byte[] data, int offset) {
        super.unpack(data, offset);

        data = this.getValue();
        offset = 0;

        this.eventSource = 0;
        offset += 0;
        this.eventType = 0;
        offset += 0;

        byte[] eventJsonBytes = new byte[this.getLength()];
        System.arraycopy(data, offset, eventJsonBytes, 0, eventJsonBytes.length);
        offset += eventJsonBytes.length;

        this.eventJson = new String(eventJsonBytes);
    }

    /**
     * @return the eventSource
     */
    public int getEventSource() {
        return eventSource;
    }

    /**
     * @param eventSource
     *            the eventSource to set
     */
    public void setEventSource(int eventSource) {
        this.eventSource = eventSource;
    }

    /**
     * @return the eventType
     */
    public int getEventType() {
        return eventType;
    }

    /**
     * @param eventType
     *            the eventType to set
     */
    public void setEventType(int eventType) {
        this.eventType = eventType;
    }

    /**
     * @return the eventJson
     */
    public String getEventJson() {
        return eventJson;
    }

    /**
     * @param eventJson
     *            the eventJson to set
     */
    public void setEventJson(String eventJson) {
        this.eventJson = eventJson;
    }
}
